﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Nizm0.QOT.Library.Forms
{
    public partial class frmQuestion : Form
    {
        private List<object> _result = new List<object>();
        private List<object> _list;

        public List<object> Result
        {
            get { return _result; }
            //set { _result = value; }
        }

        public frmQuestion(string message, string caption, List<object> list)
        {
            try
            {
                InitializeComponent();

                this.Text = caption;
                this._list = list;
                int maxtextlenght=100;
                label_Message.Text = message;
                Graphics graphics = this.CreateGraphics();
                SizeF textSize;
                if (list[0].GetType().IsSubclassOf(typeof(Form)))
                {
                    foreach (object tmpobject in list)
                    {
                        listBox_Items.Items.Add(((Form)tmpobject).ToString());
                        //Debug.WriteLine(tmpobject.ToString());
                        //listBox_Items.Items[listBox_Items.Items.IndexOf(tmpobject)]
                        textSize = graphics.MeasureString(tmpobject.ToString(), listBox_Items.Font);
                        if (maxtextlenght < textSize.Width) maxtextlenght = (Int32)System.Math.Round(textSize.Width, 0);
                    }
                }
                else
                {
                    foreach (object tmpobject in list)
                    {
                        listBox_Items.Items.Add(tmpobject);
                        //Debug.WriteLine(tmpobject.ToString());
                        //listBox_Items.Items[listBox_Items.Items.IndexOf(tmpobject)]
                        textSize = graphics.MeasureString(tmpobject.ToString(), listBox_Items.Font);
                        if (maxtextlenght < textSize.Width) maxtextlenght = (Int32)System.Math.Round(textSize.Width, 0);
                    }
                }

                listBox_Items.ColumnWidth = maxtextlenght;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), ex.Source,MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
            }
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            if (_list[0].GetType().IsSubclassOf(typeof(Form)))
            {
                //_result = new ListBox.SelectedObjectCollection(listBox_Items);
                foreach (object tmpobject in _list)
                {
                    if (listBox_Items.SelectedItems.Contains(((Form)tmpobject).ToString()))
                        _result.Add((frmResult)tmpobject);
                }
            }
            else
            {
                foreach (object tmpobject in listBox_Items.SelectedItems)
                {
                    _result.Add(tmpobject);
                }
            }

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            DialogResult = DialogResult.Cancel;
        }

        private void checkBox_SelectDeselctAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox tmpch = (CheckBox)sender;
            if (tmpch.Checked)
                for(int i = 0;i<listBox_Items.Items.Count;i++)// (string tmpobject in listBox_Items.Items)
                {
                    if (!listBox_Items.SelectedItems.Contains(listBox_Items.Items[i]))
                        listBox_Items.SelectedItems.Add(listBox_Items.Items[i]);
                }
            else
                for (int i = 0; i < listBox_Items.Items.Count; i++)
                {
                    if (listBox_Items.SelectedItems.Contains(listBox_Items.Items[i]))
                        listBox_Items.SelectedItems.Remove(listBox_Items.Items[i]);
                }
        }
    }
}
